#pragma once
#include"LogMessage.hpp"
#include<unistd.h>
#include<iostream>
#include<functional>

#define BUFF_SIZE 1024

void serverIO(int sockfd)
{  
    while(true)
    {
        char buff[BUFF_SIZE] = {0};
        ssize_t n = read(sockfd, buff, sizeof(buff)-1);

        if (n > 0)
        {
            buff[n] = '\0';
            std::cout << "recv from  client: "<< buff << std::endl;
        }
        else if (n == 0) //read返回0表示client端退出
        {
            logMessage(WARING, "client already exit");
            break;
        }

        write(sockfd, buff, n);
    }

    close(sockfd);
}

typedef std::function<void(int)> fun;

class task{
public:
    task(){}
    task(int fd, fun _fun)
    :_fd(fd), _callback(_fun) {}

    void operator()()
    {
        _callback(_fd);
    }
    
    fun  _callback;
    int _fd;
};